package com.shycloud.mido.member.quartz;

import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.service.MemberServiceV2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
public class MemberCountDetailJob implements Job {

	@Autowired
	private MemberServiceV2 memberService;
	/***/
	@Autowired
	private RedisTemplate redisTemplate;

	private static Log logger = LogFactory.getLog(MemberCountDetailJob.class);

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {

		System.out.println("开始执行获取首页数据 ===================>");

		redisTemplate.delete("countDetail-1::getCountDetail");

		List<String[]> ListStr = new ArrayList<>();

		String[] str = new String[6];
		str[0] = "product";
		str[1] = "上音社注册用户数";
		str[2] = "App注册用户数";
		str[3] = "总注册用户数";
		str[4] = "硬件码使用数量";
		str[5] = "新注册用户硬件码使用数";
		ListStr.add(str);

		//设置总数
		Integer syMemberCount = 0;
		Integer notSyMemberCount = 0;
		Integer tollMemberCount = 0;
		Integer syMemberCount998 = 0;
		Integer newMemberHardwareCount = 0;

		for (int i = 4; i >= 1; i--) {

			String[] str1 = new String[6];

			if (i == 4) {
				str1[0] = "前三周 周新增数";
			}
			if (i == 3) {
				str1[0] = "前两周 周新增数";
			}
			if (i == 2) {
				str1[0] = "前一周 周新增数";
			}
			if (i == 1) {
				str1[0] = "当前周 周新增数";
			}

			str1[1] = memberService.getSyMemberCount(i, "3");
			syMemberCount = syMemberCount + Integer.parseInt(str1[1]);
			str1[2] = memberService.getSyMemberCount(i, "0");
			notSyMemberCount = notSyMemberCount + Integer.parseInt(str1[2]);
			Integer tollCount4 = (Integer.parseInt(str1[1]) + Integer.parseInt(str1[2]));
			str1[3] = tollCount4.toString();
			tollMemberCount = tollMemberCount + Integer.parseInt(str1[3]);
			str1[4] = memberService.getSyMemberCount(i, "998");
			syMemberCount998 = syMemberCount998 + Integer.parseInt(str1[4]);
			str1[5] = memberService.getNewMemberHardwareCount(i);
			newMemberHardwareCount = newMemberHardwareCount + Integer.parseInt(str1[5]);
			ListStr.add(str1);
		}

		String[] str1 = new String[6];
		str1[0] = "当月 新增数";
		str1[1] = syMemberCount.toString();
		str1[2] = notSyMemberCount.toString();
		str1[3] = tollMemberCount.toString();
		str1[4] = syMemberCount998.toString();
		str1[5] = newMemberHardwareCount.toString();

		ListStr.add(str1);

		R res = new R(ListStr);

		redisTemplate.opsForValue().set("countDetail-1::getCountDetail", res, 2, TimeUnit.DAYS);

	}


}
